package com.cw.jeeyt.service.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cw.jeeyt.service.cache.InitCacheInfo;

/**
 * 初始化平台基础信息
 *
 * @author chenw
 */
@WebListener
public class PortalContextLoadListener implements ServletContextListener {

    private static Logger logger = LoggerFactory.getLogger(PortalContextLoadListener.class);

    @Autowired
    private InitCacheInfo initCacheInfo;

    @Override
    public void contextDestroyed(ServletContextEvent servletCtxEvent) {
    }

    /**
     * JEEYT平台基础数据初始化
     *
     * @param servletCtxEvent
     * @see
     */
    @Override
    public void contextInitialized(ServletContextEvent servletCtxEvent) {
        WebApplicationContextUtils.getRequiredWebApplicationContext(servletCtxEvent.getServletContext())
            .getAutowireCapableBeanFactory()
            .autowireBean(this);
        logger.info("---初始化JEEYT平台基础数据【开始】---");
        // 2.执行同步数据库表和配置文件的内容
        try {
            initCacheInfo.putAllCacheInfo();
        } catch (Exception e) {
            logger.error("Error when sync Portlets", e);
        }

        // 3.TODO:初始化单点登录JEEYT的信息

        // 4.TODO:生成合并的JS和CSS信息

        logger.info("---初始化JEEYT平台基础数据【结束】---");
    }

}
